﻿Public Class Form1

    Public Class MimeType
        Public Extension As String
        Public Value As String
    End Class

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '1. Parse mime types
        Dim i = inp.Text
        Dim MimeList As New List(Of MimeType)

        Dim rows = i.Split(vbCrLf)

        For Each row In rows

            Dim cols = row.Split(Chr(9))

            Dim _mime As New MimeType With {.Value = cols(1).Trim, .Extension = cols(0).Trim}

            MimeList.Add(_mime)

        Next

        '2. generates class
        Dim o As String = ""

        o &= "Public Class Mime" & vbCrLf
        o &= "Public Function GetValueByExtension(Ext as String) as String" & vbCrLf
        o &= "Select Case Ext.ToLower()" & vbCrLf

        For Each Mime In MimeList
            o &= "Case """ & Mime.Extension.ToLower & """" & vbCrLf
            o &= "Return """ & Mime.Value & """" & vbCrLf
        Next
        o &= "Case Else" & vbCrLf
        o &= "Throw New Exception (""Extension not recognized"")" & vbCrLf
        o &= "End Select" & vbCrLf
        o &= "End Function" & vbCrLf


        o &= "Public Function GetExtensionByValue(Value as String) as String" & vbCrLf
        o &= "Select Case Value.ToLower()" & vbCrLf

        For Each Mime In MimeList
            o &= "Case """ & Mime.Value.ToLower & """" & vbCrLf
            o &= "Return """ & Mime.Extension & """" & vbCrLf
        Next
        o &= "Case Else" & vbCrLf
        o &= "Throw New Exception (""Value not recognized"")" & vbCrLf
        o &= "End Select" & vbCrLf
        o &= "End Function" & vbCrLf

        o &= "End Class" & vbCrLf

        out.Text = o

        MsgBox("Done.")

    End Sub
End Class
